package com.example;

import com.example.entity.User;

public class AuthenticationService {
    private UserRepository userRepository;

    public AuthenticationService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User authenticate(String username, String password) {
        User user = userRepository.findByUsername(username);
        if (user != null && PasswordHasher.checkPassword(password, user.getPasswordHash())) {
            return user;
        }
        return null;
    }

    public boolean changePassword(User user, String oldPassword, String newPassword) {
        if (!PasswordHasher.checkPassword(oldPassword, user.getPasswordHash())) {
            return false;
        }
        user.setPasswordHash(PasswordHasher.hashPassword(newPassword));
        return true;
    }

    public UserRepository getUserRepository() {
        return userRepository;
    }
}